<template>
    <div>
        <div class="buttons">
            <el-button size="mini" @click="savePrivew">保存授权</el-button>
        </div>
        <slot><Strong>菜单信息</Strong></slot>
        <el-tree
            :data="data"
            show-checkbox
            node-key="id"
            ref="tree"
            highlight-current
            :props="defaultProps">
        </el-tree>
    </div>
</template>

<script>
import request from "../../../../../utils/request.js";
export default {
    data() {
      return {
        currentRole:{},
        data: [],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      }
    },
    methods:{
        setCheckedKeys() {
            this.$refs.tree.setCheckedKeys([3]);
        },
        queryTree(){
            const that = this;
            
            request.post("/system/query/queryMenuTree",{}).then(function(res){
                that.data = res.data;
                //加载角色权限问题 queryRoleMenus
                request.post("/system/query/queryRoleMenus",{"tsRoleId":that.currentRole.id}).then(function(res){
                    //设置树节点显示
                    that.$refs.tree.setCheckedKeys(res.data);
                });
            });
        },
        savePrivew(){
            const that = this;
            var checkedKeys = this.$refs.tree.getCheckedKeys();
            console.log("checkedKeys",checkedKeys);
            console.log("currentRole",this.currentRole);
            var data = {"menus":checkedKeys,"role":this.currentRole};
            request.post("/system/db/saveMenuRole",data).then(function(res){
                if(res.data.retCode=="200"){
                    that.$Alert({message: res.data.message,type: 'success'});
                }else{
                    that.$Alert({showClose: true,message: res.data.message,type: 'error'});
                }
            });
        }
    },
    mounted:function(){
        this.$bus.$on("showSelectTree",(obj)=>{
            this.currentRole = obj;
            //重新加载树节点
            this.$options.methods.queryTree.bind(this)();
        });

    }
}
</script>